package com.jiejie.controller;


import com.jiejie.commons.ServerResponse;
import com.jiejie.entity.Customer;
import com.jiejie.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */

@RestController
public class CustomerController {
    @Resource
    private ICustomerService customerService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //登录
    @PostMapping("customer/login")
    public ServerResponse login(Customer customer, HttpServletRequest request, HttpServletResponse response){
        System.out.println(">>>>>>>>>"+customer);
        String salt = "123456";
        customer.setCustomerPassword(DigestUtils.md5DigestAsHex((salt + customer.getCustomerPassword() +salt).getBytes()));
        ServerResponse result = customerService.login(customer);
        System.out.println("controller login response:" + result);
        if(result.getResultCode() ==200){
            String token = (String) result.getData();
            System.out.println("customer controller 登录成功");
        }
        return result;
    }

    //短信登陆
    @PostMapping("customer/loginByMsg")
    public ServerResponse loginByMsg(Customer customer,HttpServletRequest request, HttpServletResponse response){
        ServerResponse result = customerService.loginByMessage(customer);
        System.out.println("controller login response:" + result);
        if (result.getResultCode()==200){
            String token = (String) result.getData();
        }
        return result;
    }

    @PostMapping("customer/regist")
    public ServerResponse regist(Customer customer){
        System.out.println(">>>>>>>>>>>>>>>>"+customer.getCustomerName());
        String salt = "123456";
        customer.setCustomerPassword(DigestUtils.md5DigestAsHex((salt + customer.getCustomerPassword() +salt).getBytes()));
        ServerResponse result = customerService.regist(customer);
        System.out.println(result);
        if (result.getResultCode() ==200){
            System.out.println("注册成功");

        }
        return result;
    }

    @GetMapping("customer/loginSend")
    public ServerResponse sendCode(@RequestParam("phone") Long customerTel){
        System.out.println("用户手机号======"+customerTel);
        //根据手机号从redis拿验证码
        String code = stringRedisTemplate.opsForValue().get(customerTel.toString());
        if (!StringUtils.isEmpty(code)){
            return ServerResponse.success("发送成功",code);
        }
        // 如果redis 中根据手机号拿不到验证码，则生成6位随机验证码
        code = String.valueOf(UUID.randomUUID().toString().hashCode()).replaceAll("-","").substring(0,6);
        System.out.println("生成随机code:"+code);
        //验证码存入codeMap
        Map<String,Object> codeMap = new HashMap<>();
        codeMap.put("code",code);
        // 调用aliyunSendSmsService发送短信
        Boolean bool = customerService.sendMessage(customerTel,code,codeMap);
        System.out.println(code);
        if (bool){
            // 如果发送成功，则将生成的6位随机验证码存入redis缓存,5分钟后过期
            stringRedisTemplate.opsForValue().set(customerTel.toString(),code,5, TimeUnit.MINUTES);
            return ServerResponse.success("发送成功",code);
        }else {
            return ServerResponse.fail("发送失败",null);
        }
    }




    /*@GetMapping("customer")
    @ResponseBody
    public Customer getById(@RequestParam("id") Integer customerId){
        Customer customer = customerService.getById(customerId);
        return customer;
    }

    @PostMapping("saveCust")
    @ResponseBody
    public int save(Customer customer){
        customer.setCustomerRegistTime(LocalDateTime.now());
        customer.setCustomerStatus(0);
        int rows = customerService.saveCustomer(customer);
        return rows;
    }

    @DeleteMapping("deleteCust")
    @ResponseBody
    public int delete(@RequestParam("id") Integer customerId){
        int rows = customerService.deleteById(customerId);
        return rows;
    }

    @PostMapping("updateCust")
    @ResponseBody
    public int update(Customer customer){
        int rows = customerService.updateCustomer(customer);
        return  rows;
    }*/
}
